package cn.santi.controller;

import cn.santi.domain.Staff;
import cn.santi.domain.StaffDate;
import cn.santi.domain.StaffTime;
import cn.santi.emuns.R;
import cn.santi.service.StaffDateService;
import cn.santi.service.StaffService;
import cn.santi.service.StaffTimeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/design")
public class StaffDesignController {
    @Autowired
    private StaffService sff;
    @Autowired
    private StaffDateService sd;
    @Autowired
    private StaffTimeService st;

    @GetMapping("/updated")//修改预约设置
    public R updated(Staff staff) {
        System.out.println(staff);
        LambdaQueryWrapper<Staff> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Staff::getId, staff.getId());
        Staff one = sff.getOne(wrapper);
        System.out.println(one);
        if (one == null) {
            sff.save(staff);
            return R.ok("添加");
        } else {
            sff.updateById(staff);
            return R.ok("修改");
        }
    }

    @GetMapping("/showed")//显示预约设置
    public R showed(Integer id) {
//        LambdaQueryWrapper<Staff> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Staff::getId,id);
//        List<Staff> list = sff.list(wrapper);
        Staff staff = sff.getById(id);
        return R.ok(staff);
    }

    public static Boolean toBoolean(String str) {
        return "true".equalsIgnoreCase(str) || "1".equals(str);
    }

    @PostMapping("/addition")
    public R addition(@RequestBody Map<String, Object> requestBody) {
        Integer sid = (Integer) requestBody.get("sid");
        List<Map<String, Object>> map = (List<Map<String, Object>>) requestBody.get("map");

        System.out.println("SID: " + sid);
        System.out.println("Received Map: " + map);
        // 遍历每个对象
        for (Map<String, Object> item : map) {
            // 获取 isStaff
            Boolean isStaff = (Boolean) item.get("isStaff");
            System.out.println("isStaff: " + isStaff);
            // 获取 addtimes 列表
            List<Map<String, Object>> addtimes = (List<Map<String, Object>>) item.get("addtimes");

            if (isStaff) {
                Object nianObj = item.get("nian");
                Object yuefenObj = item.get("yuefen");
                Object rhaoObj = item.get("rhao");

// 使用三元运算符来安全地将字符串转换为整数
                Integer nian = nianObj instanceof Integer ? (Integer) nianObj : Integer.parseInt((String) nianObj);
                Integer yuefen = yuefenObj instanceof Integer ? (Integer) yuefenObj : Integer.parseInt((String) yuefenObj);
                Integer rhao = rhaoObj instanceof Integer ? (Integer) rhaoObj : Integer.parseInt((String) rhaoObj);

                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate localDate = LocalDate.of(nian, yuefen, rhao);
                String formattedDate = dtf.format(localDate);
                System.out.println("Formatted Date (String): " + formattedDate);

                LambdaQueryWrapper<StaffDate> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(StaffDate::getDate, formattedDate);
                wrapper.eq(StaffDate::getSid, sid);
                StaffDate one = sd.getOne(wrapper);
                if (one == null) {//新增
                    System.out.println("新增");
                    StaffDate staffDate = new StaffDate();
                    staffDate.setSid(sid);
                    staffDate.setDate(formattedDate);
                    sd.save(staffDate);
                    StaffDate one1 = sd.getOne(wrapper);
                    System.out.println("one1");
                    // 遍历 addtimes 列表
                    for (Map<String, Object> timeItem : addtimes) {
                        System.out.println("time: " + timeItem.get("time") + "time1: " + timeItem.get("time1"));

                        StaffTime staffTime = new StaffTime();
                        staffTime.setDid(one1.getId());
                        staffTime.setTime(timeItem.get("time").toString());
                        staffTime.setTime1(timeItem.get("time1").toString());
                        String str = timeItem.get("isActive").toString();
                        staffTime.setIstrue(toBoolean(str) == true ? "0" : "1");
                        st.save(staffTime);
                    }
                } else {//修改
                    System.out.println("修改");
                    StaffDate one2 = sd.getOne(wrapper);
                    System.out.println(one2);
                    LambdaQueryWrapper<StaffTime> wrapper1 = new LambdaQueryWrapper<>();
                    wrapper1.eq(StaffTime::getDid, one2.getId());
                    List<StaffTime> list = st.list(wrapper1);
                    System.out.println(list);
                    System.out.println(list.size());
                    for (int i = 0; i < addtimes.size(); i++) {
                        Map<String, Object> timeItem = addtimes.get(i);
                        System.out.println(
                                "time: " + timeItem.get("time") + "time1: " + timeItem.get("time1") + "i：" + i
                        );

                        if (list.size() <= i) {
                            System.out.println("添加1");
                            StaffTime staffTime = new StaffTime();
                            staffTime.setDid(one2.getId());
                            staffTime.setTime(timeItem.get("time").toString());
                            staffTime.setTime1(timeItem.get("time1").toString());
                            String str = timeItem.get("isActive").toString();
                            staffTime.setIstrue(toBoolean(str) == true ? "0" : "1");
                            st.save(staffTime);
                        } else {
                            StaffTime staffTime = list.get(i);
                            System.out.println(staffTime);
                            staffTime.setTime(timeItem.get("time").toString());
                            staffTime.setTime1(timeItem.get("time1").toString());
                            String str = timeItem.get("isActive").toString();
                            staffTime.setIstrue(toBoolean(str) == true ? "0" : "1");
                            st.updateById(staffTime);
                        }
                    }
                }
            } else {//判断是否更改状态
                System.out.println("更改状态");
            }
        }
        return R.ok();
    }

    @GetMapping("/shower")
    private R shower(Integer sid) {
        LambdaQueryWrapper<StaffDate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StaffDate::getSid, sid);
        List<StaffDate> list = sd.list(wrapper);
        return R.ok(list);
    }

    @GetMapping("/shower1")
    private R shower1(Integer sid) {
        StaffDate one = sd.getById(sid);
        LambdaQueryWrapper<StaffTime> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(StaffTime::getDid, one.getId());
        List<StaffTime> list = st.list(wrapper1);
        return R.ok(list);
    }

    @GetMapping("/del")
    private R del(String id) {
        // 使用逗号分隔符分割字符串
        String[] ids = id.split(",");

        // 进一步处理分割后的字符串数组，例如转换为整数
        List<Integer> idList = Arrays.stream(ids)
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        for (Integer num : idList) {
            System.out.println(num);
//            LambdaQueryWrapper<StaffTime> wrapper1 = new LambdaQueryWrapper<>();
//            wrapper1.eq(StaffTime::getId, num);
//            st.getOne(wrapper1)
            StaffTime byId = st.getById(num);
            if (byId != null){
                st.removeById(num);
            }else {
                System.out.println(num+"id不存在");
            }
        }
        System.out.println(idList);
        return R.ok();
    }
}
